#%% [markdown]
""" 
+ https://www.runoob.com/html/html-tutorial.html 请求
+ 获取侧面导航栏信息
+ 构建循环
+ 通过pandas构建表格
+ 输出展示
"""
#%%
from lxml import etree  # xpath 解析
import requests  # 网页请求
import pandas as pd  # 表格整理导出

#%% [markdown]
""" 
+ 构建请求
"""
#%%
html = requests.get(
    url="https://www.runoob.com/html/html-tutorial.html",
    headers={
        "user-agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Mobile Safari/537.36 Edg/91.0.864.41",
        "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6,ja;q=0.5",
    },
)
#%% [markdown]
""" 
+ 通过etree对请求结果进行解析
"""
text = etree.HTML(html.text)  # 通过etree对页面进行解析
#%% [markdown]
""" 
+ 通过网页的xpath工具定位到页面信息 "//div[@id='leftcolumn']/a[@target='_top']"
"""
#%%
infos = text.xpath("//div[@id='leftcolumn']/a[@target='_top']")  # xpath
#%% [markdown]
""" 
+ 构建提取信息的循环
"""
#%%
con = []  # 存储输出值
for n in infos:
    d = {}
    key = n.xpath("./text()")[0].strip()
    urls = n.xpath("./@href")[0]
    con.append(dict(name=key, urls="https://www.runoob.com" + urls))
#%% [markdown]
""" 
+ 整理导出打印
"""
#%%
df = pd.DataFrame(con)
display(df)
df.to_excel("oup_put.xls", sheet_name="runoob_html教程")
# %%

